package com.ktjy.controller;

import com.ktjy.dto.WorkFlowDTO;
import com.ktjy.pojo.TbActivity;
import com.ktjy.service.IWorkFlowService;
import com.ktjy.utils.AjaxResult;
import com.ktjy.utils.PageUtil;
import com.ktjy.vo.ActCommentEntity;
import com.ktjy.websocket.WebSocketServer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/workFlow")
@Tag(name = "用户模块")
@Slf4j
public class WorkFlowController {
    @Autowired
    IWorkFlowService workFlowService;
    @Autowired
    WebSocketServer webSocketServer;

    @Operation(summary = "启动流程")
    @PutMapping("/startProcess/{id}")
    public AjaxResult startProcess(@PathVariable("id") Integer id) {
        try {
            String processDefinitionKey = "Activity2";
            workFlowService.startProcess(processDefinitionKey, id);
            webSocketServer.send("收到一个待办任务，请及时处理");
            return AjaxResult.success("启动流程成功");
        } catch (Exception e) {
            log.error("启动流程失败", e);
            return AjaxResult.error("启动流程失败");
        }
    }

    @Operation(summary = "查询当前登录用户的待办任务")
    @GetMapping("/loadTask")
    public AjaxResult loadTask(WorkFlowDTO workFlowDTO,
                               @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            PageUtil pageUtil = workFlowService.findTask(workFlowDTO, pageNo, pageSize);
            return AjaxResult.success(pageUtil);
        } catch (Exception e) {
            log.error("查询失败", e);
            return AjaxResult.error("查询失败");
        }
    }

    @Operation(summary = "根据任务ID查询业务数据")
    @GetMapping("/loadTask/{taskId}")
    public AjaxResult loadTask(@PathVariable("taskId") String taskId) {
        try {
            TbActivity activity = workFlowService.findActivityByTaskId(taskId);
            return AjaxResult.success(activity);
        } catch (Exception e) {
            log.error("查询失败", e);
            return AjaxResult.error("查询失败");
        }
    }

    @Operation(summary = "根据任务ID查询批注信息")
    @GetMapping("/loadComment/{taskId}")
    public AjaxResult loadComment(@PathVariable("taskId") String taskId){
        try{
            List<ActCommentEntity> comments = workFlowService.findCommentByTaskId(taskId);
            return AjaxResult.success(comments);
        }catch (Exception e){
            log.error("查询失败",e);
            return AjaxResult.error("查询失败");
        }
    }

    @Operation(summary = "完成任务")
    @PutMapping("/completeTask")
    public AjaxResult completeTask(@RequestBody WorkFlowDTO workFlowDTO){
        try{
            workFlowService.completeTask(workFlowDTO);
            return AjaxResult.success("任务完成成功");
        }catch (Exception e){
            log.error("任务完成失败",e);
            return AjaxResult.error("任务完成失败");
        }
    }
}
